
let StreakControl =require("StreakControl")
cc.Class({
    extends: cc.Component,

    properties: {

        root:{
            default:null,
            type:cc.Node,
        },

        bar:{
            default:null,
            type:cc.Node,
        },

        streak:{
            default:null,
            type:StreakControl,
        
        },

        velocity:{
            default:cc.Vec2.ZERO,
        },

        rigidBody:{
            default:null,
            type: cc.RigidBody,
        },
        linerSpeedAdd:7,//线性增加值

        Speed:1,//增长率
        maxSpeed:2,
        rateOfIncrease:0.01,
        _isPlaying:false,
       

    },

    // LIFE-CYCLE CALLBACKS:

     onLoad () {
        this.rigidBody =this.node.getComponent(cc.RigidBody);
        this.streak =this.node.getComponentInChildren(StreakControl);
    
     },

    start () {


    },

    StartGame(startVelocity){

        this._isPlaying =true;

        this.rigidBody.linearVelocity =startVelocity; //new cc.Vec2(0,500);//指数速度增长还是会过快
        this.schedule(function(){

            this.Speed+=this.rateOfIncrease;

            if(this.Speed>this.maxSpeed/2)
            {
                this.streak.OpenStreak();
            }

            if(this.Speed>=this.maxSpeed)
            {
                this.Speed =this.maxSpeed;

                return;
            }


            this.velocity =this.rigidBody.linearVelocity;
            let normal =this.velocity.normalize();//获得其方向

            //this.velocity.addSelf(normal*10);

            this.velocity.x+=normal.x*this.linerSpeedAdd;
            this.velocity.y+=normal.y*this.linerSpeedAdd;


            //this.velocity.x +=this.velocity.x* this.Speed;
            //this.velocity.y +=this.velocity.y* this.Speed;
            this.rigidBody.linearVelocity =this.velocity;

        },7);
    },

    onCollisionEnter:function(other,self)
    {

    },

     update (dt) {

         if(!this._isPlaying)
         {
             this.node.x =this.bar.x;
         }
     },


     
     onBeginContact: function (contact, selfCollider, otherCollider) {
    },

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact: function (contact, selfCollider, otherCollider) {
    },

    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve: function (contact, selfCollider, otherCollider) {
    },

    // 每次处理完碰撞体接触逻辑时被调用
    onPostSolve: function (contact, selfCollider, otherCollider) {
        
       
        //console.log("碰撞点");

        //速度

        let temp =(this.Speed)/this.maxSpeed;
        //console.log("temp"+temp);
        //console.log("temp111"+(255-255*temp));
        let thiscolor= this.node.color.setG((255-255*temp));
        thiscolor.b =(255-255*temp);
        this.node.color =thiscolor;
        //console.log("color"+this.node.color);

    },

    //将球变大
    BallBigger(time){

        //默认扩大1倍；
        let currentWidth= this.node.width*2;
        let h =this.node.height*2;
        this.node.width =currentWidth;
        this.node.height=h;

        let boxCollder= this.node.getComponent(cc.PhysicsCircleCollider);
        boxCollder.radius =currentWidth/2;
        boxCollder.apply();

        this.scheduleOnce(function(){
            boxCollder.radius =currentWidth/4;


            boxCollder.apply();
            this.node.width=currentWidth/2;
            this.node.height=currentWidth/2;
        },time);

    },



    AddBallSpeed()
    {

    },

    DelBallSpeed()
    {

    },


    GetRigidVelocity(){
    return this.rigidBody.linearVelocity;
    },

});
